package cn.kgc.bdqn_studentinfomanager.service.ManagerService.impl;

import cn.kgc.bdqn_studentinfomanager.dao.manager.ManagerMapper;
import cn.kgc.bdqn_studentinfomanager.pojo.common.Const;
import cn.kgc.bdqn_studentinfomanager.pojo.common.ServerResponse;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.AddUserVo;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.ProvinceAndCityVo;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.ReturnManagerInfoVo;
import cn.kgc.bdqn_studentinfomanager.service.ManagerService.ManagerService;
import cn.kgc.bdqn_studentinfomanager.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * by Shanshui CC on 2020/12/10 14:32
 */
@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ManagerMapper managerMapper;

    /**
     * 点击页面时 加载省份列表
     *
     * @return
     */
    @Override
    public ServerResponse loadingProvince() {
        List<ProvinceAndCityVo> provinceVoList = managerMapper.queryProvince();

        if (provinceVoList == null) {
            return ServerResponse.createByErrorMessage("查询省份列表失败");
        }
        return ServerResponse.createBySuccess(provinceVoList);
    }

    /**
     * 管理员添加渠道主管
     *
     * @param addUserVo
     * @param token
     * @return
     */
    @Override
    public ServerResponse addManagerUser(AddUserVo addUserVo, String token) {

        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));
        //添加父级Id
        addUserVo.setParentId(id);

        //添加职位Id
        addUserVo.setRole(addUserVo.getRole() + "");

        //如果职位是咨询师 将地址置空
        if (Integer.parseInt(addUserVo.getRole()) == Const.UserRole.FIVE) {
            addUserVo.setAddressId(null);
        }

        //加密密码
        addUserVo.setUserPassword(MD5Util.MD5EncodeUtf8(addUserVo.getUserPassword()));
        //进行添加
        Integer integer = managerMapper.addManagerUserOrCommUserOrConsult(addUserVo);

        if (integer >= 1) {
            return ServerResponse.createBySuccessMessage("添加渠道主管成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }


    /**
     * 渠道主管添加渠道专员
     *
     * @param addUserVo
     * @param token
     * @return
     */
    @Override
    public ServerResponse addCommUser(AddUserVo addUserVo, String token) {

        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //添加父级Id
        addUserVo.setParentId(id);
        //添加职位Id
        addUserVo.setRole(3 + "");

        Integer integer = managerMapper.addManagerUserOrCommUserOrConsult(addUserVo);

        if (integer >= 1) {
            return ServerResponse.createBySuccessMessage("添加渠道专员成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }


    @Override
    public ServerResponse selectbydate(String token, String selectDate) {

        //将Date类型的时间转化为String型的。
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//        String stringDate = sdf.format(selectDate);

        //学生总数
        Integer studentCount = 0;
        //成功学生总数
        Integer studentSuccessCount = 0;

        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数为空");
        }
        //从token中拿到用户id
        String parentId = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //临时对象
        ReturnManagerInfoVo returnManagerInfoVo = new ReturnManagerInfoVo();
        //最后返回的集合
        List<ReturnManagerInfoVo> returnManagerInfoVoList = new ArrayList<>();
        //通过管理员Id 获取所有主管Id
        List<String> ExecList = managerMapper.queryExecIdByManagerId(parentId);

        for (String ExId : ExecList) {
            int i = 0;
            //查主管id 名字 地址
            returnManagerInfoVo = managerMapper.queryManagerInfoById(ExId);
            //通过主管id 查询所有专员id
            //专员id集合
            List<String> CommIdList = managerMapper.queryCommByManagerId(ExId);

            //通过专员集合  查询所有招生老师id
            //招生老师id集合
            if (CommIdList.size() != 0) {
                List<String> RecrIdList = managerMapper.queryRecrByCommId(CommIdList);

                if (RecrIdList != null) {
                    //学生总数
                    studentCount = managerMapper.queryStudentCount(RecrIdList, CommIdList, selectDate);
                    //成功总数
                    studentSuccessCount = managerMapper.queryStudentSuccessCount(CommIdList, RecrIdList, selectDate);
                }
            }
            returnManagerInfoVo.setStudentCount(studentCount);
            returnManagerInfoVo.setStudentSuccessCount(studentSuccessCount);
            returnManagerInfoVoList.add(returnManagerInfoVo);
            studentCount = 0;
            studentSuccessCount = 0;
        }
        return ServerResponse.createBySuccess(returnManagerInfoVoList);
    }

    @Override
    public ServerResponse selectExec(String userName, String token) {
        //招生老师的学生
        Integer recrCount = 0;
        //招生老师成功的学生
        Integer cecrSuccessCount = 0;

        if (StringUtils.isBlank(token)) {
            return ServerResponse.createByErrorMessage("参数为空");
        }
        //从token中拿到用户id
        String parentId = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //临时对象
        ReturnManagerInfoVo returnManagerInfoVo = new ReturnManagerInfoVo();
        //最后返回的集合
        List<ReturnManagerInfoVo> returnManagerInfoVoList = new ArrayList<>();
        //所有渠道专员
        List<String> CommList = managerMapper.queryCommByExecId(parentId, userName);


        for (String commList : CommList) {
            //所有招生老师
            List<String> RecrList = managerMapper.queryRecrIdByCommId(commList);

            //专员id 姓名 地址
            returnManagerInfoVo = managerMapper.queryCommInfoById(commList);
            //专员的学生
            Integer commCount = managerMapper.queryStudentCountByCommId(commList);
            //专员成功的学生
            Integer commSuccessCount = managerMapper.queryStudentSuccessCountByCommId(commList);
            if (RecrList.size() != 0) {
                //招生老师的学生
                recrCount = managerMapper.queryStudentCountByRecrList(RecrList);
                //招生老师成功的学生
                cecrSuccessCount = managerMapper.queryStudentSuccessCountByRecr(RecrList);
            }
            returnManagerInfoVo.setStudentCount(commCount + recrCount);
            returnManagerInfoVo.setStudentSuccessCount(commSuccessCount + cecrSuccessCount);
            returnManagerInfoVoList.add(returnManagerInfoVo);
        }
        return ServerResponse.createBySuccess(returnManagerInfoVoList);
    }


    @Override
    public ServerResponse addConsultUser(AddUserVo addUserVo, String token) {

        //从token中拿到用户id
        String id = StringUtils.substring(
                token, token.indexOf(":") + 1, token.indexOf("-"));

        //添加父级Id
        addUserVo.setParentId(id);
        //添加职位Id
        addUserVo.setRole(5 + "");

        Integer integer = managerMapper.addManagerUserOrCommUserOrConsult(addUserVo);

        if (integer >= 1) {
            return ServerResponse.createBySuccessMessage("添加咨询师成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }
}
